package com.yzh.blog.handler;

import com.yzh.blog.constant.MessageConstant;
import com.yzh.blog.entity.Result;
import com.yzh.blog.pojo.Article;
import com.yzh.blog.service.inter.ArticleService;
import com.yzh.blog.utils.QiNiuTools;
import com.yzh.blog.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yzh
 * @version 1.0
 * @description: TODO
 * @date 2022/4/14 20:40
 */
@RequestMapping("/article")
@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章列表
     * @return
     */
    @RequestMapping("/articleList.do")
    @ResponseBody
    public Result getArticleList() {
        return articleService.getArticleList();
    }

    /**
     * 获取所有文章类型
     * @return
     */
    @RequestMapping("/getArticleTypes.do")
    @ResponseBody
    public Result getArticleTypes() {
        return articleService.getArticleTypes();
    }

    /**
     * 上传单张图片
     * @param imgFile 图片文件
     * @return
     */
    @RequestMapping("/uploadImg.do")
    @ResponseBody
    public Result uploadImg(MultipartFile imgFile) {
        try {
            // 获取原始名称，改为uuid的名称
            String fileName = Tools.originalFilename2UUIDName(imgFile.getOriginalFilename());
            // 调用七牛云服务，上传图片
            QiNiuTools.upload2Qiniu(imgFile.getBytes(), fileName);
            return new Result(true, MessageConstant.UPLOAD_IMG_SUCCESS, fileName);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new Result(false, MessageConstant.UPLOAD_IMG_FAIL);
        }
    }

    /**
     * 上传多张图片
     * @param imgs 图片列表 前端提交formData
     * @return
     */
    @RequestMapping("/uploadImgs.do")
    @ResponseBody
    public Result uploadImgs(@RequestParam("imgs") MultipartFile[] imgs) {
        try {
            String[] images2FrontEnd = new String[imgs.length];
            for (int i = 0; i < imgs.length; i++) {
                // 获取原始名称，改为uuid的名称
                String fileName = Tools.originalFilename2UUIDName(imgs[i].getOriginalFilename());
                // 将名称赋给前端
                images2FrontEnd[i] = fileName;
                QiNiuTools.upload2Qiniu(imgs[i].getBytes(), fileName);
            }
            return new Result(true, MessageConstant.UPLOAD_IMG_SUCCESS, images2FrontEnd);
        } catch (Exception e) {
            return new Result(false, MessageConstant.UPLOAD_IMG_FAIL);
        }
    }

    /**
     * 添加文章
     * @param map 文章对象
     * @return
     */
    @RequestMapping("/addArticle.do")
    @ResponseBody
    public Result addArticle(@RequestBody Map<String, Object> map) {
        return articleService.addArticle(map);
    }
}
